<template>
  <breadcrumb :levelList="levelList" />
  <el-card class="card">
    <el-form v-if="state.showSearch==true" :inline="true" :model="queryForm" class="demo-form-inline">
      <el-form-item label="名称">
        <el-input v-model="queryForm.post_name" placeholder="岗位名称" clearable />
      </el-form-item>
      <el-form-item>
      <el-button type="primary" @click="getDataList">查询</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
         <el-button type="primary" plain size="small" @click="addHandle(dataFormRef)" >新增</el-button>
      </el-col>
      <el-col :span="1.5">
         <el-button type="danger" plain size="small" :disabled="multiple" @click="handleDeleteBatch" >删除</el-button>
      </el-col>
    </el-row>
    <el-table :data="state.dataList" style="width: 100%" @selection-change="selectionChangeHandle">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column prop="post_name" label="岗位名称" width="180" />
      <el-table-column prop="post_code" label="岗位编码" width="180" />
      <el-table-column label="状态" align="center" width="100">
         <template #default="scope">
          <el-button v-if="scope.row.status=='0'" size="small" type="primary" plain>正常</el-button>
          <el-button v-if="scope.row.status=='1'" size="small" type="warning" plain>禁用</el-button>
         </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" @click="editHandle(scope.row)">编辑</el-button>
          <el-button size="small"  type="danger"  @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination small background layout="prev, pager, next ,total, sizes" :total="state.total" v-model:page-size="state.limit" @change="pageChangeHandle" />
  </el-card>
  <el-dialog :title="state.dialogTitle" v-model="state.showDialog" width="600px" append-to-body>
    <el-form :model="state.dataForm" ref="dataFormRef" :rules="rules"  label-width="120px">
      <el-form-item label="岗位编号" prop="post_code">
        <el-input placeholder="请输入岗位编号" v-model="dataForm.post_code" />
      </el-form-item>
      <el-form-item label="岗位名称" prop="post_name">
        <el-input placeholder="请输入岗位名称" v-model="dataForm.post_name" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio-button label="正常" value="0" />
          <el-radio-button label="禁用" value="1" />
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" type="textarea" placeholder="请输入内容" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit(dataFormRef)">确 定</el-button>
          <el-button @click="cancelHandle">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive, ref, toRefs, onMounted } from 'vue'
import { CrudTool } from '@/utils/curd'
import { getPostApiUrl } from '@/api/sys/post'
import breadcrumb from '@/views/common/breadcrumb.vue'

const dataFormRef = ref();
const levelList = ['首页','系统设置','岗位管理']

const state = reactive({
  primaryKey:'post_id',
  queryForm: {
    post_name:''
  },
  apiUrl: getPostApiUrl(),
  total:0,
  dataForm:{
    post_id: 0,
    post_code: undefined,
    post_name: undefined,
    status: '0',
    remark: undefined
  },
  titleText:['添加岗位','修改岗位'],
  rules:{
    post_code: [{ required: true, message: "岗位编码不能为空", trigger: "blur" },{ pattern: /^\d{2,8}$/, message: "请输入正确的岗位编码", trigger: "blur" }],
    post_name: [{ required: true, message: "岗位名称不能为空", trigger: "blur" }],
  }

});

const { queryForm, dataForm, rules } = toRefs(state);
const { getDataList, addHandle, editHandle, cancelHandle, pageChangeHandle, selectionChangeHandle, handleSubmit, handleDelete, handleDeleteBatch } = CrudTool(state)

getDataList()

</script>

<style scoped>
.card{
  min-height:600px;
}
</style>